轻松聘
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

154 lines
4.2 KiB

import { Ionicons } from '@expo/vector-icons';
import { useLocalSearchParams, useRouter } from 'expo-router';
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';
import type { ComponentProps } from 'react';
import { SafeAreaView } from 'react-native-safe-area-context';
import { useGrowthData } from '../../src/hooks';
import { LoadingState, ErrorState, EmptyState } from '../../src/components/StateView';
import { colors } from '../../src/theme';
import { formatViews } from '../../src/utils';
import type { Article } from '../../src/types';
type IconName = ComponentProps<typeof Ionicons>['name'];
const THUMBNAIL_BG: Record<Article['thumbnailColor'], string> = {
violet: colors.infoLight,
orange: colors.actionLight,
green: colors.successLight,
};
const THUMBNAIL_COLOR: Record<Article['thumbnailColor'], string> = {
violet: colors.info,
orange: colors.action,
green: colors.success,
};
/* ===================== 顶部导航 ===================== */
function Header({ onBack }: { onBack: () => void }) {
return (
<View style={styles.header}>
<Pressable style={styles.headerBtn} onPress={onBack} hitSlop={8}>
<Ionicons name="chevron-back" size={24} color={colors.textPrimary} />
</Pressable>
<Text style={styles.headerTitle}>职场干货</Text>
<View style={styles.headerBtn} />
</View>
);
}
/* ===================== 页面 ===================== */
export default function ArticleScreen() {
const router = useRouter();
const { id } = useLocalSearchParams<{ id: string }>();
const { articles, loading, error, refresh } = useGrowthData();
const article = articles.find((a) => a.id === id);
return (
<SafeAreaView style={styles.safeArea} edges={['top']}>
<Header onBack={() => router.back()} />
{loading ? (
<LoadingState text="加载文章中…" />
) : error ? (
<ErrorState onRetry={refresh} />
) : !article ? (
<EmptyState icon="document-text-outline" title="文章不存在" subtitle="该内容可能已下架" />
) : (
<ScrollView
style={styles.scroll}
contentContainerStyle={styles.content}
showsVerticalScrollIndicator={false}
>
<View style={[styles.thumb, { backgroundColor: THUMBNAIL_BG[article.thumbnailColor] }]}>
<Ionicons
name={article.icon as IconName}
size={40}
color={THUMBNAIL_COLOR[article.thumbnailColor]}
/>
</View>
<Text style={styles.title}>{article.title}</Text>
<View style={styles.meta}>
<Text style={styles.tag}>{article.tag}</Text>
<Text style={styles.views}>{formatViews(article.views)}阅读</Text>
</View>
<Text style={styles.body}>{article.content}</Text>
</ScrollView>
)}
</SafeAreaView>
);
}
const styles = StyleSheet.create({
safeArea: {
flex: 1,
backgroundColor: colors.pageBg,
},
header: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
paddingHorizontal: 8,
height: 48,
backgroundColor: colors.white,
borderBottomWidth: 1,
borderBottomColor: colors.borderLight,
},
headerBtn: {
width: 40,
height: 40,
alignItems: 'center',
justifyContent: 'center',
},
headerTitle: {
fontSize: 17,
fontWeight: '700',
color: colors.textPrimary,
},
scroll: {
flex: 1,
},
content: {
padding: 16,
paddingBottom: 24,
},
thumb: {
width: 72,
height: 72,
borderRadius: 16,
alignItems: 'center',
justifyContent: 'center',
marginBottom: 14,
},
title: {
fontSize: 20,
fontWeight: '700',
color: colors.textPrimary,
lineHeight: 28,
},
meta: {
flexDirection: 'row',
alignItems: 'center',
marginTop: 10,
marginBottom: 16,
},
tag: {
fontSize: 11,
color: colors.primaryText,
backgroundColor: colors.primaryLight,
paddingHorizontal: 8,
paddingVertical: 2,
borderRadius: 8,
overflow: 'hidden',
marginRight: 10,
},
views: {
fontSize: 12,
color: colors.textHint,
},
body: {
fontSize: 14,
lineHeight: 24,
color: colors.textSecondary,
},
});